<?php

namespace App\Repositories\Policy\Eloquent;

use App\Repositories\Policy\Contracts\FzPolicyInterface;
use App\Services\TaoBaoSdk\domain\DatePairDTO;
use App\Services\TaoBaoSdk\domain\FlightDTO;
use App\Services\TaoBaoSdk\domain\PassengerDto;
use App\Services\TaoBaoSdk\domain\PolicyCreateRequestDto;
use App\Services\TaoBaoSdk\domain\PolicyDto;
use App\Services\TaoBaoSdk\domain\PolicyQueryParamDTO;
use App\Services\TaoBaoSdk\domain\PriceDto;
use App\Services\TaoBaoSdk\domain\SaleDTO;
use App\Services\TaoBaoSdk\request\AlitripPolicySpecialUploadRequest;
use App\Services\TaoBaoSdk\TopClient;

class FzPolicyRepository implements FzPolicyInterface
{

    private $session = '6101b114074eeb8bbb91d39e6ae5fdf26e0a6a6afb014762212084556742';

    /**
     * 政策列表
     * @param $flight
     * @param $cabin
     * @param $policy
     * @return PolicyDto
     */
    public function upload($flight,$cabin,$policy){


        $policy_list = new PolicyDto;
        $policy_list->airline=$flight['hs_code'];
        $policy_list->arr_airport=$flight['depart_arrived_code'];
        //$policy_list->create_pnr="1";
        $policy_list->dep_airport=$flight['depart_city_code'];
        $flights = new FlightDTO;
        $flights->allow_flight_num=$flight['flight_code'];
        $allow_travel_date = new DatePairDto;
        $allow_travel_date->end=$flight['date'];
        $allow_travel_date->start=$flight['date'];
        $flights->allow_travel_date = $allow_travel_date;
        if (!empty($policy['time_enable']) && strpos($policy['time_enable'],'-'))
        $flights->allow_travel_time= $policy['time_enable'];
        $flights->cabin=$cabin['cabin_code'];
        $flights->cabin_class="Y";
        $flights->day_week= explode(',',$policy['week_enable']);
        $flights->flight_index="0";
        //$flights->restrict_flight_num="1111-2222,3333";
        //$restrict_travel_date = new DatePairDto;
        //$restrict_travel_date->end="2024-04-03";
        //$restrict_travel_date->start="2024-04-06";
        //$flights->restrict_travel_date = $restrict_travel_date;
        //$flights->allow_flight_num2="8320";
        //$flights->restrict_flight_num2="8320";
        $policy_list->flights = $flights;
        $policy_list->office_no= $policy['office'];
        $policy_list->policy_code=$policy['policy_code'];
        $policy_list->policy_source="2";
        $price = new PriceDTO;
        $price->commission="0";
        $price->return_price="0";
        $price->out_price =  $cabin['price'] + $policy['add_price'];
        $price->bidd_fee= intval($policy['rule_set'][0]['biding_price']) * 100;
        $price->bidd_fee_percent= intval($policy['rule_set'][0]['biding_percentage']) * 100;
        $policy_list->price = $price;

        $sale = new SaleDTO;
        $sale->advance_day=$policy['last_sale']."-".$policy['begin_sale'];
        $sale_date = new DatePairDTO;
        $sale_date->end=$policy['rule_end'];
        $sale_date->start=$policy['rule_at'];
        $sale->sale_date = $sale_date;
        //$sale->sale_time="0606-0707,0808-0909";
        //$sale->stay_day="5-10";
        $sale->sale_mode_code="0";
        //$sale->goods_market="[0,1]";
        //$sale->stay_time_unit="0";
        $policy_list->sale = $sale;

        $passenger = new PassengerDto;
        $passenger->documents_type=$policy['id_card'] == 'ID' ? 1 : null;
        //$passenger->documents_limit="41";
        $passenger->product_code= $policy['product_type'] == 4 ? 0 : 0;
        /*$passenger->age_limit="15-24";
        $passenger->pax_num="1-3";*/
        $policy_list->passenger = $passenger;

        $policy_list->fare_price = $policy['add_price'] + $cabin['price'];
        $policy_list->status="2";
        $policy_list->is_white="0";
        $policy_list->memo="注意事项";
        //$policy_list->not_appl_od="[\"KKK-*\",\"*-KKK\",\"KKK-LLL\"]";
        $policy_list->pata="1";
        $policy_list->code_share="0";
        //$policy_list->code_share_airline="HU,AU";
        $policy_list->direct_transfer_type="0";
        $policy_list->out_main_flight="false";
        //$policy_list->fare_source="[0,1]";
        $policy_list->urgent_stop_sell=$policy['filter_nervous'];
        $policy_list->create_pnr_limit="1";

        return $policy_list;
    }

    /**
     * 删除
     * @return PolicyQueryParamDTO
     */
    public function delete($flight,$cabin,$policy){

        $delete_policy = new PolicyQueryParamDTO;

        $delete_policy->airline=$flight['hs_code'];
        $delete_policy->arr_airport=$flight['depart_arrived_code'];
        $delete_policy->cabin_list1=$cabin['cabin_code'];
        //$delete_policy->cabin_list2="M";
        $delete_policy->dep_airport=$flight['depart_city_code'];
        $delete_policy->fuzzy_query="false";
        $delete_policy->policy_code=$policy['policy_code'];
        $delete_policy->policy_source="2";
        $delete_policy->sale_start_date=$policy['depart_at'];
        $delete_policy->sales_end_date=$policy['depart_end'];
        $delete_policy->status=$flight['status'] == 1 ? : 2;
        $delete_policy->travel_end_date=$flight['date'];
        $delete_policy->travel_start_date=$flight['date'];

        return $delete_policy;
    }

    /**
     * 上传飞猪
     * @param $policy_list
     * @param $delete_policy
     * @return bool
     */
    public function uploadPolicy($policy_list,$delete_policy)
    {

        $c = new TopClient;
        $c->appkey = '33508647';
        $c->secretKey = 'd86753cb8ede20f328bdf06f9cd583c3';

        $req = new AlitripPolicySpecialUploadRequest;

        $param_policy_create_request_d_t_o = new PolicyCreateRequestDto;
        $param_policy_create_request_d_t_o->agent_id="5460";
        $param_policy_create_request_d_t_o->delete_policy = $delete_policy;
        $param_policy_create_request_d_t_o->exec_type="ADD";

        $param_policy_create_request_d_t_o->policy_list = $policy_list;
        $param_policy_create_request_d_t_o->policy_type="T";
        $param_policy_create_request_d_t_o->trip_type="0";
        $param_policy_create_request_d_t_o->agent_send_time=date("Y-m-d H:i:s");
        $req->setParamPolicyCreateRequestDTO(json_encode($param_policy_create_request_d_t_o));

        $resp = $c->execute($req,$this->session);
        if ($resp->result && $resp->result->success) {

            return true;
        }

        return false;

    }
}